package jmind.asteroidea.core;

import com.google.common.collect.Sets;

import java.lang.reflect.Method;
import java.util.Set;

/**
 * description:
 *
 * @author weibo.xie
 * @date:2022/7/14 上午11:27
 */
public class AsteroideaUtils {


    public static final String[] createParamArgs(Object[] args) {
        if(args==null){
            return null;
        }
        int length=args.length;
        String[] argsNames=new String[length];
        for(int i=0;i<length;i++){
            argsNames[i]="arg"+i;
        }
        return argsNames;
    }

    public static boolean isToStringMethod(Method method) {
        return "toString".equals(method.getName()) && method.getParameterTypes().length == 0;
    }
    /**
     * 以下方法如果不做处理，将会死循环。因为是对接口直接代理。
     * @param method
     * @return
     */
    public static boolean isSpecialMethod(Method method) {
        if(!SPECIAL_NAME.contains(method.getName())) {
            return false;
        }

        Class<?>[] parameterTypes = method.getParameterTypes();
        switch(method.getName()) {
            // equals(Object)
            case "equals" :
                if(parameterTypes.length == 1 && parameterTypes[0] == Object.class) {
                    return true;
                }
                break;
            // hashCode()
            case "hashCode":
                // notify()
            case "notify":
                // notifyAll()
            case "notifyAll":
                if(parameterTypes.length == 0) {
                    return true;
                }
                break;
            // wait(), wait(Long), wait(Long, Integer)
            case "wait" :
                if(parameterTypes.length == 0) {
                    return true;
                }
                if(parameterTypes.length == 1 && parameterTypes[0] == Long.class) {
                    return true;
                }
                if(parameterTypes.length == 2 && parameterTypes[0] == Long.class && parameterTypes[1] == Integer.class) {
                    return true;
                }
                break;
            default:
                break;
        }
        return false;
    }

    private static final Set<String> SPECIAL_NAME = Sets.newHashSet("equals", "hashCode", "notify", "notifyAll", "wait");
}
